package com.ice.body.dict.controller;
import cn.hutool.core.bean.BeanUtil;
import com.ice.body.business.model.ReagentAreaModel;
import com.ice.body.business.model.WareHouseModel;
import com.ice.body.business.service.ReagentAreaService;
import com.ice.body.business.service.WareHouseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import com.ice.base.controller.BaseController;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.ice.base.result.ResponseResult;
import com.ice.base.result.AjaxResult;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
/**
 * Created with GuoYanBin.
 * Description:
 * Date: 2021-06-15
 * Time: 01:56
 */
@RestController
@ApiIgnore(value = "业务接口-试剂摆放区")
public class ReagentAreaController extends BaseController{
    //---------------------------业务接口-试剂摆放区-----------------------start
    @Resource
    private ReagentAreaService reagentAreaService;
    @Resource
    private WareHouseService wareHouseService;

    @ApiOperation(value = "业务接口-试剂摆放区-新增")
    @PostMapping("/api/ReagentArea/insert")
    public AjaxResult<ReagentAreaModel> insert_ReagentArea(ReagentAreaModel model) {
        AjaxResult r = ResponseResult.success();
        try{
            String warehouseId = model.getWarehouseId();
            if(StringUtils.isNotEmpty(warehouseId)){
                WareHouseModel wareHouseModel = wareHouseService.selectById(warehouseId);
                if(BeanUtil.isNotEmpty(wareHouseModel)){
                    model.setWarehouseName(wareHouseModel.getWarehouseName());
                }
            }

            model.setCreateTime(new Date());
            reagentAreaService.insert(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "业务接口-试剂摆放区-删除")
    @PostMapping("/api/ReagentArea/deleteById")
    public AjaxResult<ReagentAreaModel> deleteById_ReagentArea(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
            reagentAreaService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "业务接口-试剂摆放区-更新")
    @PostMapping("/api/ReagentArea/updateById")
    public AjaxResult<ReagentAreaModel> update_ReagentArea(ReagentAreaModel model) {
        AjaxResult r = ResponseResult.success();
        try{
            WareHouseModel wareHouseModel = wareHouseService.selectById(model.getWarehouseId());
            model.setWarehouseName(wareHouseModel.getWarehouseName());
            reagentAreaService.update(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "业务接口-试剂摆放区-查询一个")
    @PostMapping("/api/ReagentArea/selectById")
    public AjaxResult<ReagentAreaModel> selectById_ReagentArea(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
           ReagentAreaModel model = reagentAreaService.selectById(id);
           r.setData(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "业务接口-试剂摆放区-查询多个")
    @PostMapping("/api/ReagentArea/selectAll")
    public AjaxResult<ReagentAreaModel> selectList_ReagentArea(ReagentAreaModel model) {
        AjaxResult r = ResponseResult.success();
        try{
           List<ReagentAreaModel> list = reagentAreaService.selectListByModel(model);
           r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "业务接口-试剂摆放区-查询分页")
    @PostMapping("/api/ReagentArea/selectListByPageHelper")
    public AjaxResult<ReagentAreaModel> selectListByPageHelper_ReagentArea(@RequestParam(name = "pageSize") Integer pageSize,
                                                                           @RequestParam(name = "pageNum") Integer pageNum,
                                                                           ReagentAreaModel model){
        AjaxResult r = ResponseResult.success();
        ArrayList list;
        try{
           PageHelper.startPage(pageNum, pageSize);
           list = reagentAreaService.selectListByModel(model);
           getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
    //---------------------------业务接口-试剂摆放区-----------------------end
}
